package com.appgole.base;

import com.appgole.http.ClientConfig;
import io.restassured.RestAssured;
import io.restassured.config.EncoderConfig;
import io.restassured.config.HeaderConfig;
import io.restassured.config.HttpClientConfig;
import io.restassured.filter.log.LogDetail;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import org.testng.annotations.*;

import java.io.File;

import static io.restassured.RestAssured.*;

/**
 * Created by Administrator on 2019/2/24.
 */
public class BaseCase {
    protected final String fileName = getClass().getSimpleName();
    /**
     * 测试报告文件(html)的完整路径
     */
    protected final String htmlFilePath = getOutputFolder() + fileName + ".html";
    /**
     * 测试报告文件用于发送email的(html)的完整路径
     */
    protected final String emailFilePath = getOutputFolder() + "email-" + fileName + ".html";

    public BaseCase() {
        File folder = new File(getOutputFolder());
        if (!folder.exists()) {
            folder.mkdirs();
        }
    }

    @BeforeClass
    public void setup() {
        baseURI = ClientConfig.baseUri;  //请求IP
        basePath = ClientConfig.basePath;
        enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.ALL);
        /**
         * 设置通用的请求头
         */
        config = config().httpClient(HttpClientConfig.httpClientConfig().addParams(ClientConfig.httpClientParams));
        /**
         *设置编码为'utf-8'
         */
        config = config().encoderConfig(EncoderConfig.encoderConfig().defaultContentCharset("UTF-8"));
        /**
         * 设置代理
         */
        if (ClientConfig.enableProxy) {
            proxy(ClientConfig.proxyHost, ClientConfig.proxyPort);
        }
        /**
         * 日志信息
         */
        replaceFiltersWith(new RequestLoggingFilter(), new ResponseLoggingFilter());
    }

    @BeforeSuite(description = "BeforeSuite")
    public void beforeSuite() {

    }


    @BeforeGroups(description = "BeforeGroups")
    public void beforeGroups() {

    }

    @BeforeTest(description = "BeforeTest")
    public void beforeTest() {

    }

    @BeforeMethod(description = "BeforeMethod")
    public void beforeMethod() {

    }


    @AfterMethod(description = "AfterMethod")
    public void afterMethod() {

    }

    @AfterTest(description = "AfterTest")
    public void afterTest() {

    }


    @AfterSuite(description = "AfterSuite")
    public void afterSuite() {

    }

    @AfterGroups(description = "AfterGroups")
    public void afterGroups() {

    }


    @AfterClass(description = "AfterClass")
    public void tearDown() {

    }


    /**
     * 设置测试报告文件夹路径
     *
     * @return
     */
    protected String getOutputFolder() {
        return "test-output/";
    }


}
